package com.zpkj.portal.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zpkj.portal.dto.Page;
import com.zpkj.portal.dto.RequestParameter;
import com.zpkj.portal.dto.ResponseParameter;
import com.zpkj.portal.entity.UserMain;
import com.zpkj.portal.service.news.NewsService;
import com.zpkj.portal.service.user.UserMainService;
import com.zpkj.portal.utils.Constant;
import com.zpkj.portal.utils.JsonUtils;
import com.zpkj.portal.utils.SignUtil;

/**
 * @author KK
 * 快报接口
 */
@RestController
@RequestMapping("/news")
public class NewsController {
	
	@Autowired
	private NewsService newsService;
	
	@Autowired
	private UserMainService userMainService;

	/**
	 * 获取好球快报
	 */
	@ResponseBody
	@RequestMapping(value = "/getGoodNews")
	public String getGoodNews(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
		RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
		//返回值
		ResponseParameter result = new ResponseParameter("getGoodNews",params.getHeader(),requestSource);
		
		if (StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)) {
			return result.addError(Constant.ReturnCode.CODE_1999, "签名验证错误").toJson();
		}
		String userId = params.getValueFromBody("loginUserId");
		if (StringUtils.isEmpty(userId)) {
			return result.addError(Constant.ReturnCode.CODE_1001, "参数为空").toJson();
		}
		Map<String, Object> retMap = newsService.queryGoodNews();
		UserMain userMain = userMainService.getUserMainByUserId(userId);
		
		retMap.put("userMain", userMain);
		return result.addData(retMap).setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}
	
	/**
	 * 获取资讯列表
	 */
	@ResponseBody
	@RequestMapping(value = "/getNewsPageList")
	public String getNewsPageList(HttpServletRequest request,
			@RequestParam(defaultValue = "", required = false) String requestSource, @RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		
		// 请求参数
		RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class, requestSource);
		// 返回值
		ResponseParameter result = new ResponseParameter("getNewsPageList", params.getHeader(), requestSource);
		// 验证签名
		if (StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)) {
			return result.addError(Constant.ReturnCode.CODE_1999, "签名验证错误").toJson();
		}
		// 页码
		String currPage = params.getValueFromBody("currPage") == null ? Constant.CURRPAGE
				: params.getValueFromBody("currPage");
		// 每页条数
		String pageSize = params.getValueFromBody("pageSize") == null ? Constant.PAGESIZE
				: params.getValueFromBody("pageSize");
		int no = Integer.valueOf(currPage);
		int size = Integer.valueOf(pageSize);
		if (!(no > 0)) {
			return result.addError(Constant.ReturnCode.CODE_2003, "参数 currPage 值不正确").toJson();
		}
		if (!(size > 0)) {
			return result.addError(Constant.ReturnCode.CODE_2003, "参数 pageSize 值不正确").toJson();
		}
		int start = (no - 1) * size;
		List<Map<String, Object>> retList = newsService.queryPageForList(start, size);
		long totalCount = 0;
		Page page = new Page(no, size, totalCount);
		return result.addData(retList).addPage(page).setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}
	
	/**
     * 接口调用测试
     */
    @ResponseBody
	@RequestMapping(value = "/inokeTest")
	public String inokeTest(@RequestParam(name="a") Integer a,@RequestParam(name="b") Integer b) throws Exception {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("a", a);
    	map.put("b", b);
    	map.put("ret", "success");
        return JsonUtils.toJson(map);
    }
}
